<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>迭代对象</title>
</head>

<body>

    <script>
        // 迭代一个对象的数组
        // let interatorObj = {
        //     friend: ['zs', 'ls', 'lt', 'wys', 'ssy'],
        //     [Symbol.iterator]: function () {  // [Symbol.iterator]是固定的  这个函数必须返回一个对象  这个对象必须包含
        //         let index = 0
        //         let interator = {
        //             next: () => {
        //                 index++;
        //                 if (index <= this.friend.length) {
        //                     return {
        //                         value: this.friend[index - 1],
        //                         done: false
        //                     }
        //                 } else {
        //                     return {
        //                         done: true
        //                     }
        //                 }
        //             }
        //         }
        //         return interator;
        //     }
        // }

        // const interator = interatorObj[Symbol.iterator]();
        // console.log(interator.next());
        // console.log(interator.next());
        // console.log(interator.next());
        // console.log(interator.next());
        // console.log(interator.next());
        // console.log(interator.next());


        // for (const value of interatorObj) {
        //     console.log(value);
        // }

        // let arr = ['a', 'b', 'c', 'd', 'e']
        // let arrInterator = arr[Symbol.iterator]()
        // console.log(arrInterator.next());
        // console.log(arrInterator.next());
        // console.log(arrInterator.next());
        // console.log(arrInterator.next());
        // console.log(arrInterator.next());
        // console.log(arrInterator.next());


        // 迭代一个对象的key/value
        let obj = {
            name:'zs',
            age:18,
            gender:'男',
            height:1.88,
            [Symbol.iterator]:function(){
                let index = 0
                // let keys = Object.keys(this);
                // let values = Object.values(this);
                let entries = Object.entries(this);
                let interator = {
                   next:() => {
                    if(index < entries.length){
                        return {value:entries[index++],done:false}
                    }else{
                        return {value:undefined,done:true}
                    }
                   } 
                }  
                return interator;
            }
        }
        for (const value of obj) {
            console.log(value);
        }



    </script>

</body>

</html>